//
//  AppDelegate.m
//  fff
//
//  Created by ZLQ on 2016/11/3.
//  Copyright © 2016年 hbyj. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "CCLockViewController.h"
#import "CCLockViewController.h"
#import "WJTouchID.h"
#import "Util.h"

#import "WXApi.h"
#import "WXApiManager.h"
@interface AppDelegate ()<WJTouchIDDelegate,WXApiDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:SCREEN];
    
    
    //如果是模拟器
    if (TARGET_IPHONE_SIMULATOR) {
        self.window.rootViewController = [[MainViewController alloc] init];
    }else{
        
        self.window.rootViewController = [[MainViewController alloc] init];
        
        UserEntity *userId = [Util getCurrentUserInfo];
        if (userId.isLogin){
            CCLockViewController *lockVC = [[CCLockViewController alloc] init];
            lockVC.czmm = @"1";
            self.window.rootViewController = lockVC;
            [[WJTouchID touchID] startWJTouchIDWithMessage:WJNotice(@"请指纹解锁", @"The Custom Message") fallbackTitle:WJNotice(@"按钮标题", @"Fallback Title") delegate:self];
        }
    }
    
    [self.window makeKeyAndVisible];
    
    //向微信注册wxd930ea5d5a258f4f
    [WXApi registerApp:@"wxb4ba3c02aa476ea1" withDescription:@"demo 2.0"];
    
    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
//    return [WXApi handleOpenURL:url delegate:self];
//    return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
    //return [WXApi handleOpenURL:url delegate:self];
    //return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    //NSLog(@"应用程序进入后台");
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

//每次进入前台 都会调用这个方法,每次都需要 指纹 解锁
- (void)applicationWillEnterForeground:(UIApplication *)application {
    //NSLog(@"应用程序进入前台");
    
    UserEntity *userId = [Util getCurrentUserInfo];
    if (userId.isLogin) {
        CCLockViewController *lock = [[CCLockViewController alloc] init];
        [UIApplication sharedApplication].keyWindow.rootViewController = lock;
        
        [[WJTouchID touchID] startWJTouchIDWithMessage:WJNotice(@"请指纹解锁", @"The Custom Message") fallbackTitle:WJNotice(@"按钮标题", @"Fallback Title") delegate:self];
    }
    
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

/**
 *  TouchID验证成功
 *
 *  (English Comments) Authentication Successul
 */
- (void)WJTouchIDAuthorizeSuccess{
    MainViewController *root=[[MainViewController alloc]init];
    [UIApplication sharedApplication].delegate.window.rootViewController = root;
    //[UIApplication sharedApplication].keyWindow.rootViewController = root;
}

/**
 *  用户点击了取消按钮
 *
 *  Authentication Failure
 */
- (void) WJTouchIDAuthorizeErrorUserCancel {
    
    //[[CCLockViewController new] performSelector:@selector(changeView) withObject:self afterDelay:2];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Lock" object:nil];
    
}

/**
 *  TouchID验证失败
 *
 *  (English Comments) Authentication Failure
 */
- (void)WJTouchIDAuthorizeFailure{
    NSLog(@"指纹验证失败");
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
